import 'dart:convert';

import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import '../service/resouceService.dart';

class themePage extends StatefulWidget {
  @override
  _themePage createState() => _themePage();
}

class _themePage extends State<themePage> {
  List bgImgList = [];

  var _futureBuilderFuture;

  // 獲取主題數據
  Object getThemeData() async {
    var params={
        "pageNum":1,
        "pageSize":10,
        "type":1
    };
    var response = await resouceService.list(params);
    setState(() {
      this.bgImgList = response.data;
    });
    print(response);
    return null;
  }

  @override
  void initState() {
    _futureBuilderFuture = getThemeData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: BrnAppBar(
          title: Text('主題列表',style: TextStyle(color: Colors.black),),
          backgroundColor: Color.fromARGB(255, 237, 236, 236),
          // elevation: 0,
        ),
        body: FutureBuilder(
            future: _futureBuilderFuture,
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              print(snapshot.data);
              return RefreshIndicator(
                onRefresh: () async{
                    getThemeData();
                },
                child:    Container(
                  child: Column(
                children: [
                  Container(
                    height: MediaQuery.of(context).size.height * 0.8,
                    padding: EdgeInsets.only(top: 10),
                    child: ListView.builder(
                        itemCount: this.bgImgList.length,
                        itemBuilder: (context, index) {
                          return GFListTile(
                            title:
                                Text(this.bgImgList[index]['title'].toString()),
                            subTitleText:
                                this.bgImgList[index]['desc'].toString(),
                            avatar: GFAvatar(
                              backgroundImage:
                                  NetworkImage(this.bgImgList[index]['src']),
                            ),
                            icon: GFButton(
                              text: '選擇',
                              onPressed: () {
                                print(bgImgList);
                                // 選擇主題
                                Navigator.of(context).pushNamedAndRemoveUntil(
                                    '/calculator',
                                    arguments: "${bgImgList[index]['src']}",
                                    (Route<dynamic> route) => false);
                              },
                            ),
                            color: Color.fromARGB(95, 188, 188, 188),
                          );
                        }),
                  ),
              ])
              ));
            }
            )
            );
  }
}
